import {
	fileURLToPath,
	URL
} from 'node:url'
const path = require('path')
import {
	defineConfig,
	loadEnv
} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import {
	ElementPlusResolver
} from 'unplugin-vue-components/resolvers'
import {
	createHtmlPlugin
} from 'vite-plugin-html'


import {
	viteCommonjs
} from '@originjs/vite-plugin-commonjs';

export default defineConfig(({
	command,
	mode
}) => {
	const env = loadEnv(mode, process.cwd(), '')
	return {
		base: process.env.NODE_ENV === 'production' ? './' : '/admin/',
		// base: './admin',
		build: {
			assetsDir: 'assets' // 静态资源输出目录
		},
		plugins: [
			vue(),
			createHtmlPlugin({
				inject: {
					data: env,
				},
				entry: '/src/main.js',
				template: 'index.html',
			}),
			AutoImport({
				resolvers: [ElementPlusResolver()],
			}),
			Components({
				resolvers: [ElementPlusResolver()],
			}),
			viteCommonjs()
		],
		resolve: {
			alias: {
				"_public": path.resolve(__dirname, "./public"),
				'@': fileURLToPath(new URL('./src',
					import.meta.url)),
				'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
			},
			extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
		},
		server: {
			host: '0.0.0.0',
			port: env.VITE_APP_PORT,
			proxy: {
				"api": {
					target: env.VITE_APP_API_BASEURL,
					changeOrigin: true,
					ws: true,
					rewrite: (path) => path.replace(/^\/api/, ""),
				}
			}
		}
	}
})
